package com.itheima.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.SettingsManager;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class SettingsController {

    @Autowired
    private SettingsManager settingsManager;

    //查询用户的通知信息
    @GetMapping("/users/settings")
    public SettingVo findMySettings() {
        return settingsManager.findMySettings();

    }

    //保存或更新陌生人问题
    @PostMapping("/users/questions")
    public Map saveOrUpdateQuestions(@RequestBody Map<String,String> map) {
        //1.接收参数
        String content = map.get("content");
        //2.调用manager
        settingsManager.saveOrUpdateQuestions(content);
        return null;

    }

    //保存或者更新推送设置
    @PostMapping("/users/notifications/setting")
    public void saveOrUpdateNotifications(@RequestBody Notification notificationParam) {
        //1.调用manager
        settingsManager.saveOrUpdateNotifications(notificationParam);
    }

    //黑名单查询
    @GetMapping("/users/blacklist")
    public PageBeanVo findBlackList(@RequestParam(defaultValue = "1", name = "page") Integer pageNumber, @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize) {
//        System.out.println("黑名单数据为：" + settingsManager.findBlackList(pageNumber, pageSize));
        return settingsManager.findBlackList(pageNumber, pageSize);
    }

    //删除黑名单用户   rest风格
        ///users/blacklist/变量
    @DeleteMapping("/users/blacklist/{blackUserId}")
    public Map deleteBlackUser(@PathVariable Long blackUserId) {

        return settingsManager.deleteBlackUser(blackUserId);
    }




    //修改手机号- 1 发送短信验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendSms() {
        User user = UserHolder.get();
        String phone = user.getPhone();
       settingsManager.sendSms(phone);
    }

    //修改手机号-2   校验验证码
    @PostMapping("/users/phone/checkVerificationCode")
    public Map<String, Boolean> verifyCode(@RequestBody Map<String,String> map) {
        String verificationCode = map.get("verificationCode");

        return settingsManager.verifyCode(verificationCode);
    }


    //修改手机号 - 3 保存
    @PostMapping("/users/phone")
    public void savePhone(@RequestBody Map<String,String> map) {
        User user = UserHolder.get();
        String phone = map.get("phone");
        user.setPhone(phone);
        settingsManager.updatePhoneByUserId(user,phone);

    }


}
